package com.base.cn.platform.os.controller.web.teacher;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.course.CourseType;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.service.course.cou.CourseService;
import com.base.cn.platform.os.service.course.qa.question.QaQuestionService;
import com.base.cn.platform.os.service.manage.count.PageViewCountService;
import com.base.cn.platform.os.service.manage.teacher.SysTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 前台讲师Controller
 *
 * @author s.li
 * @create 2018-05-10-19:43
 */
@Controller
public class WebTeacherController extends WebBaseController {

    @Autowired
    private SysTeacherService sysTeacherService;
    @Autowired
    private QaQuestionService qaQuestionService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private PageViewCountService pageViewCountService;


    /**
     * 初始化讲师列表页面
     * @return
     */
    @RequestMapping("/web/teacher/list")
    public String teacherList(Model model,
                              @RequestParam(value =  "isQuestionOrder",required = false,defaultValue = "0") int isQuestion,
                              @RequestParam(value =  "teacherIdentity",required = false,defaultValue = "1") String teacherIdentity){
        model.addAttribute("isQuestionOrder",isQuestion);
        model.addAttribute("teacherIdentity",teacherIdentity);
        return resultPage("/teacher/teacher-list");
    }

    /**
     * 加载讲师数据
     * @param model
     * @param params
     * @param currentPage
     * @return
     */
    @RequestMapping("/web/teacher/ajaxTeacherList")
    public String ajaxTeacherList(Model model,
                                  @RequestBody Map<String,Object> params,
                                  @RequestParam("currentPage") int currentPage){
        params.put("status",1);
        Map<String,Object> dataMap = sysTeacherService.findWebTeacherListPage(params,currentPage,8,false,false,false);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/teacher/teacher-ajax-list");
    }

    /**
     * 讲师详情
     * @param teacherId 讲师ID
     * @return 详情页面
     */
    @RequestMapping("/web/teacher/info")
    public String teacherInfo(Model model,
                              @RequestParam("teacherId")BigDecimal teacherId){
        Map<String,Object> teacher = sysTeacherService.findSysTeacherById(teacherId,false,false,false, true, false);
        model.addAttribute("teacher",teacher);
        model.addAttribute("mobilePathShare", "/mobile/teacher/teacherInfo?teacherId="+teacher.get("id"));
        return resultPage("/teacher/teacher-info");
    }

    /**
     * 获取所有的讲师
     * @return Map<String,Object>
     */
    @RequestMapping("/web/teacher/findAllTeacherList")
    @ResponseBody
    public Map<String,Object> findAllTeacherList(@RequestParam(value =  "teacherIdentity",required = false,defaultValue = "") String teacherIdentity){
        Map<String,Object> params = new HashMap<>();
        params.put("teacherIdentity",teacherIdentity);
        List<Map<String,Object>> teacherList = sysTeacherService.findSysTeacherList(params,false,false,false, false, false);
        return ResultUtil.SUCCESS("讲师查询成功",teacherList);
    }

    /**
     * 通过讲师ID，获取讲师数据
     * @param teacherId 课程ID
     * @return Map<String,Object>
     */
    @RequestMapping("/web/uc/teacher/findQaTeacherById")
    @ResponseBody
    public Map<String,Object> findQaTeacherById(@RequestParam("teacherId") BigDecimal teacherId){
        Map<String,Object> teacher = sysTeacherService.findQaTeacherById(teacherId);
        return ResultUtil.SUCCESS("查询成功",teacher);
    }

    /**
     * 查询讲师的问题列表
     * @param model Model
     * @param request HttpServletRequest
     * @param teacherId 讲师ID
     * @return String
     */
    @RequestMapping("/web/teacher/findTeacherQuestionList")
    public String findTeacherQuestionList(Model model, HttpServletRequest request,
                                          @RequestParam("teacherId") BigDecimal teacherId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> params = new HashMap<>();
        params.put("teacherId", teacherId);
        params.put("selectFrom",2);
        Map<String, Object> dataMap = qaQuestionService.queryQuestionListPage(1, 4, params, userId, false, false, true, false, false, false, false,false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/teacher/teacher-question-list");
    }

    /**
     * 查询讲师的课程列表
     * @param model Model
     * @param request HttpServletRequest
     * @param currentPage 当前页码
     * @param params 查询条件
     * @return
     */
    @RequestMapping("/web/teacher/findTeacherCourseList")
    public String findTeacherCourseList(Model model, HttpServletRequest request,
                                        @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                        @RequestBody Map<String, Object> params){
        params.put("type",1);
        params.put("notExpire",true);
        params.put("status",1);
        params.put("orderType",1);
        params.put("notCourseTypeKey", CourseType.COURSE_TYPE_PACKAGE.getKey());
        Map<String, Object> dataMap = courseService.findWebCoursePage(params, currentPage, 6, false, false, false, true,false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/teacher/teacher-course-list");
    }


    /**
     * 验证是否存在问答教师
     * @return
     */
    @RequestMapping("/web/teacher/isQuestion")
    @ResponseBody
    public Map teacherIsQuestionCount(){
        Map<String,Object> params = new HashMap<>();
        params.put("isQuestion",1);
        params.put("status",1);
        boolean falg = false;
        BigDecimal bigDecimal = sysTeacherService.countTeacher(params);
        if(bigDecimal.compareTo(new BigDecimal(0))!=0){
            falg =true;
        }
        return ResultUtil.SUCCESS("获取成功",falg);
    }
}
